package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.reggie.constant.JwtClaimsConstant;
import com.reggie.constant.MessageConstant;
import com.reggie.dto.EmployeeDTO;
import com.reggie.dto.EmployeeLoginDTO;
import com.reggie.dto.EmployeePageQueryDTO;
import com.reggie.entity.Employee;
import com.reggie.exception.AccountLockedException;
import com.reggie.properties.JwtProperties;
import com.reggie.result.PageResult;
import com.reggie.result.R;
import com.reggie.service.EmployeeService;
import com.reggie.utils.JwtUtil;
import com.reggie.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 测试拦截器
     * */
    @GetMapping("/testJwt")
    public R<String> testJwt(){
        return R.success();
    }

    /**
     * 1.员工登录
     * */
    @PostMapping("/login")
    public R<EmployeeLoginVO> login(HttpServletRequest request, @RequestBody EmployeeLoginDTO employeeLoginDTO){
        Employee employee = employeeService.login(employeeLoginDTO);

        //生成jwt令牌
        Map<String,Object> claims =new HashMap();

        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        //jwt令牌
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(),jwtProperties.getAdminTtl(),claims);

        //返回给前段展示的VO类
        EmployeeLoginVO employeeLoginVO =EmployeeLoginVO.builder()
                .id(employee.getId())
                .name(employee.getName())
                .token(token)
                .userName(employee.getUsername())
                .build();
        return R.success(employeeLoginVO);
    }


    /**
     * 2.员工登出
     * */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success();
    }


    /**
     * 3.员工注册
     * */
    @PostMapping
    public R<String> reg(HttpServletRequest request, @RequestBody Employee employee){
        //查询该用户名是否存在
        LambdaQueryWrapper<Employee> queryWrapper =new LambdaQueryWrapper<>();
        //
        LambdaUpdateWrapper<Employee> updateWrapper=new LambdaUpdateWrapper<>();

        queryWrapper.eq(Employee::getUsername,employee.getUsername());

        Employee emp=employeeService.getOne(queryWrapper);

        //如果存在
        if (emp!=null){
            throw new AccountLockedException(MessageConstant.USERNAME_ALREADY_EXISTS);
        }
        //把前端传来的密码进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.reg(employee);
        return R.success();
    }

    /**
     * 4.显示员工页面
     * */
    @GetMapping("/page")
    public R<PageResult> page (EmployeePageQueryDTO employeePageQueryDTO){
        PageResult pageResult=employeeService.pageQuery(employeePageQueryDTO);
        return R.success(pageResult);
    }

    /**
     * 5.修改员工状态
     * */
    @PostMapping("/status/{status}")
    public R<String> startOrStop(@PathVariable("status") Integer status,Long id){
        employeeService.startOrStop(status,id);
        return R.success();
    }

    /**
     * 6.根据ID查询用户信息
     * */
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable("id") long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }


    /**
     * 7.修改员工信息
     * */
    @PutMapping
    public R<String> update(@RequestBody EmployeeDTO employeeDTO){
        Employee employee= new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);

        employeeService.updateById(employee);

        return R.success();
    }
}
